﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ArsenalManagement.Models;

namespace ArsenalManagement.Controllers
{
    public class UserinfoController : Controller
    {
        private readonly ArsenalManagementContext _context;

        public UserinfoController(ArsenalManagementContext context)
        {
            _context = context;
        }

        // GET: Userinfo
        public async Task<IActionResult> Index()
        {
            var arsenalManagementContext = _context.Userinfos.Include(u => u.PrimaryweaponNavigation).Include(u => u.User).Include(u => u.VehicleNavigation).Include(u => u.VesselNavigation);
            return View(await arsenalManagementContext.ToListAsync());
        }

        // GET: Userinfo/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null || _context.Userinfos == null)
            {
                return NotFound();
            }

            var userinfo = await _context.Userinfos
                .Include(u => u.PrimaryweaponNavigation)
                .Include(u => u.User)
                .Include(u => u.VehicleNavigation)
                .Include(u => u.VesselNavigation)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (userinfo == null)
            {
                return NotFound();
            }

            return View(userinfo);
        }

        // GET: Userinfo/Create
        public IActionResult Create()
        {
            ViewData["Primaryweapon"] = new SelectList(_context.Weapons, "Id", "Id");
            ViewData["Userid"] = new SelectList(_context.Users, "Id", "Id");
            ViewData["Vehicle"] = new SelectList(_context.Vehicles, "Id", "Id");
            ViewData["Vessel"] = new SelectList(_context.Vessels, "Id", "Id");
            return View();
        }

        // POST: Userinfo/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Userid,Primaryweapon,Vehicle,Vessel")] Userinfo userinfo)
        {
            if (ModelState.IsValid)
            {
                _context.Add(userinfo);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["Primaryweapon"] = new SelectList(_context.Weapons, "Id", "Id", userinfo.Primaryweapon);
            ViewData["Userid"] = new SelectList(_context.Users, "Id", "Id", userinfo.Userid);
            ViewData["Vehicle"] = new SelectList(_context.Vehicles, "Id", "Id", userinfo.Vehicle);
            ViewData["Vessel"] = new SelectList(_context.Vessels, "Id", "Id", userinfo.Vessel);
            return View(userinfo);
        }

        // GET: Userinfo/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null || _context.Userinfos == null)
            {
                return NotFound();
            }

            var userinfo = await _context.Userinfos.FindAsync(id);
            if (userinfo == null)
            {
                return NotFound();
            }
            ViewData["Primaryweapon"] = new SelectList(_context.Weapons, "Id", "Id", userinfo.Primaryweapon);
            ViewData["Userid"] = new SelectList(_context.Users, "Id", "Id", userinfo.Userid);
            ViewData["Vehicle"] = new SelectList(_context.Vehicles, "Id", "Id", userinfo.Vehicle);
            ViewData["Vessel"] = new SelectList(_context.Vessels, "Id", "Id", userinfo.Vessel);
            return View(userinfo);
        }

        // POST: Userinfo/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Userid,Primaryweapon,Vehicle,Vessel")] Userinfo userinfo)
        {
            if (id != userinfo.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(userinfo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserinfoExists(userinfo.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["Primaryweapon"] = new SelectList(_context.Weapons, "Id", "Id", userinfo.Primaryweapon);
            ViewData["Userid"] = new SelectList(_context.Users, "Id", "Id", userinfo.Userid);
            ViewData["Vehicle"] = new SelectList(_context.Vehicles, "Id", "Id", userinfo.Vehicle);
            ViewData["Vessel"] = new SelectList(_context.Vessels, "Id", "Id", userinfo.Vessel);
            return View(userinfo);
        }

        // GET: Userinfo/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null || _context.Userinfos == null)
            {
                return NotFound();
            }

            var userinfo = await _context.Userinfos
                .Include(u => u.PrimaryweaponNavigation)
                .Include(u => u.User)
                .Include(u => u.VehicleNavigation)
                .Include(u => u.VesselNavigation)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (userinfo == null)
            {
                return NotFound();
            }

            return View(userinfo);
        }

        // POST: Userinfo/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            if (_context.Userinfos == null)
            {
                return Problem("Entity set 'ArsenalManagementContext.Userinfos'  is null.");
            }
            var userinfo = await _context.Userinfos.FindAsync(id);
            if (userinfo != null)
            {
                _context.Userinfos.Remove(userinfo);
            }
            
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool UserinfoExists(int id)
        {
          return (_context.Userinfos?.Any(e => e.Id == id)).GetValueOrDefault();
        }
    }
}
